<template>
    <div :id="id" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import resize from '../../../assets/js/charts/resize'
export default {
    mixins: [resize],
    props: {
        rankOption: {},
        className: {
            type: String,
            default: 'chart'
        },
        id: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '200px'
        },
        height: {
            type: String,
            default: '200px'
        }
    },
    data() {
        return {
            chart: null
        }
    },
    mounted() {
        this.initChart()
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart() {
            var titlename = this.rankOption.titlename;
            var myColor = this.rankOption.myColor;
            var parcentData = this.rankOption.parcentData;
            var valdata = this.rankOption.valdata;
            this.chart = echarts.init(document.getElementById(this.id));
            this.chart.setOption({
                grid: {
                    top: '8%',
                    left: '10%',
                    right: '10%',
                    bottom: '8%',
                    containLabel: true
                },
                yAxis: [
                    {
                        show: true,
                        data: titlename,
                        inverse: true,
                        axisLine: {
                            show: false
                        },
                        splitLine: {
                            show: false
                        },
                        axisTick: {
                            show: false
                        },
                        axisLabel: {
                            margin: 20,
                            textStyle: {
                                fontSize: 12,
                                // align:'left',
                                color: '#000'
                            }
                        },
                    },
                    {
                        show: true,
                        inverse: true,
                        data: valdata,
                        axisLabel: {
                            margin: 20,
                            textStyle: {
                                fontSize: 12,
                                color: '#000',
                            },
                            formatter: (value, index) => {
                                // console.log(value, index)
                                return Math.round(parcentData[index] / value*100) + "%";
                            },
                        },
                        axisLine: {
                            show: false
                        },
                        splitLine: {
                            show: false
                        },
                        axisTick: {
                            show: false
                        },

                    }
                ],
                xAxis: [{
                    type: 'value',
                    axisLine: {
                        lineStyle: {
                            color: "transparent"
                        }
                    },
                    axisLabel: {
                        show: false
                    },

                    splitLine: {
                        show: false
                    }
                }],
                series: [{
                    type: 'bar',
                    barWidth: 10,
                    data: parcentData,
                    label: {
                        normal: {
                            show: true,
                            position: 'insideBottomRight',
                            formatter: '{c}',
                            distance: 0,
                            offset: [20, -10],
                            color: '#888',
                            fontSize: 12,
                            padding: [5, 15, 10, 15],
                            // backgroundColor: {
                            //   image: ''
                            // }
                        }
                    },
                    itemStyle: {
                        normal: {
                            barBorderRadius: 10,
                            color: function (params) {
                                var num = myColor.length;
                                return myColor[params.dataIndex % num]
                            },

                        }

                    }
                }, {
                    name: '条',
                    type: "bar",
                    barWidth: 10,
                    xAxisIndex: 0,
                    barGap: "-100%",
                    data: valdata,
                    itemStyle: {
                        normal: {
                            color: "#D7D7D7",
                            barBorderRadius: 10
                        }
                    },
                    zlevel: -1
                },
                ]
            })
        }
    }
}
</script>
